package com.xd56.dao.ticket;

import java.util.List;

import org.springframework.stereotype.Repository;

import net.villion.framework.jdbcorm.support.BaseJdbcDaoSupport;

import com.xd56.pojo.common.TourismPlan;
import com.xd56.pojo.food.TouristInfo;
import com.xd56.pojo.guide.Guide;
import com.xd56.pojo.guide.GuideInfo;
import com.xd56.pojo.guide.GuideSign;
import com.xd56.pojo.ticket.PlanUser;
import com.xd56.pojo.ticket.Ticket;

@Repository
public class TicketDaoImpl extends BaseJdbcDaoSupport implements ITickDao {

	@Override
	public Ticket findTicketByPlan(  Long id ,Long scenicId) {
		String sql = "select a.* from t_book_ticket as a where a.plan_id = ?";
		List<Ticket> lis = queryListBySql(sql, new Object[] { id },
				Ticket.class);
		if (lis != null && !lis.isEmpty()) {
			for (Ticket t : lis) {
				if (t.getScenicId().equals(scenicId)) {
					return t;
				}
			}
			return lis.get(0);

		} else {
			return null;
		}

	}
	
	@Override
	public Ticket findTicketByIdcader(String Idcard, Long scenicId) {
		String sql = "select * from t_book_ticket as a "
				+ "left join t_Indep_tourist as b on a.id = b.ticket_id "
				+ "left join t_tourist_info as c on b.tourist_id = c.id "
				+ "where c.card_no = ? and a.scenic_id =?";
		List<Ticket> lis = queryListBySql(sql,
				new Object[] { Idcard, scenicId }, Ticket.class);
		if (lis == null || lis.isEmpty()) {
			return null;
		} else {
			return lis.get(0);
		}

	}

	@Override
	public TouristInfo findTouristInfoByIdCard(String Idcard) {
		String sql = "select * from t_tourist_info as a where a.card_no = ?";
		List<TouristInfo> lis = queryListBySql(sql, new Object[] { Idcard },
				TouristInfo.class);
		if (lis == null || lis.isEmpty()) {
			return null;
		} else {
			return lis.get(0);
		}
	}

	public List<GuideSign> findGuideSign(Long planId, int type, Long gId) {
		String sql = "select * from t_guide_sign as a wahere a.plan_id = ? and a.sign_type =? and a.guideid = ?";
		List<GuideSign> lis = queryListBySql(sql, new Object[] { planId, type,
				gId }, GuideSign.class);
		return lis;

	}

	@Override
	public GuideInfo querybycardID(String cardID) {

		if (cardID != null && !cardID.equals("")) {

			String sql = "select * from t_guide_info where idcard = ?";
			List<GuideInfo> list = queryListBySql(sql, new Object[] { cardID },
					GuideInfo.class);
			if (list != null && list.size() > 0) {

				return list.get(0);
			}
		}

		return null;
	}
	
	@Override
	public TourismPlan querybyGID(String cardID) {

		if (cardID != null && !cardID.equals("")) {

			String sql = "SELECT a.* FROM t_tourism_plan AS a LEFT JOIN t_book_guide AS b ON b.plan_id = a.id LEFT JOIN t_guide_info AS c ON c.id = b.guide_id WHERE c.idcard = ? ";
//			GuideInfo gi= querybycardID(cardID);
//			String sql1="select a.* from t_book_guide as a where a.guide_id = ?";
//			List<Guide> list = queryListBySql(sql1, new Object[] { gi.getId() },
//					Guide.class);
//			String sql2="select a.* from t_tourism_plan AS a where a.id = ?";
//			List<TourismPlan> list2 = queryListBySql(sql2, new Object[] {list.get(0).getId() },
//					TourismPlan.class);
//			if (list2 != null && list2.size() > 0) {
//
//				return list2.get(0);
//			}
			List<TourismPlan> list2 = queryListBySql(sql, new Object[] {cardID },
					TourismPlan.class);
						if (list2 != null && list2.size() > 0) {
				
							return list2.get(0);
						}
		}

		return null;
	}
	@Override
	public List<PlanUser> findAllTouristInfoByplan(Long planId){
		String sql = "select a.* from t_tourism_user as a where a.plan_id = ? ";
		List<PlanUser> list = queryListBySql(sql, new Object[] { planId },
				PlanUser.class);
		return list;
	}
	
	@Override
	public List<GuideSign> findSign(Long planId,Long placeId,Long guideId){
		String sql = "select a.* from t_guide_sign as a where a.plan_id = ? and sign_type = ? and placeid = ? and guideid = ?";
		List<GuideSign> list = queryListBySql(sql, new Object[] { planId,GuideSign.TICKET ,placeId,guideId}, 
				GuideSign.class);
		return list;
	}
}
